﻿Public Class FormMaterialesListado

    Dim classController As New Class_RC_Controller.rc_Materiales()

    Private m_frmMaterial As FrmMateriales
    Public Property frmMateriales() As FrmMateriales
        Get
            Return m_frmMaterial
        End Get
        Set(value As FrmMateriales)
            m_frmMaterial = value
        End Set
    End Property

    Private Sub FormMaterialesListado_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        dgMateriales.AutoGenerateColumns = False
        CargarCombos()
        CargarMateriales()
    End Sub

    Private Sub CargarMateriales()

        Dim codTipo As Integer
        If cbxTipo.Checked Then
            codTipo = cboTipoMaterial.SelectedValue
        End If


        Dim dt As New DataTable
        dt = classController.get_Materiales_All(oiml.dbConn, 0, txtMaterial.Text, codTipo)
        dgMateriales.DataSource = dt

    End Sub

    Private Sub CargarCombos()
        Dim dsTipo As New DataTable
        dsTipo = classController.get_tipo_Materiales(oiml.dbConn)
        If Not dsTipo Is Nothing Then
            If dsTipo.Rows.Count > 0 Then
                cboTipoMaterial.DataSource = dsTipo
                cboTipoMaterial.DisplayMember = "DescTipoMaterial"
                cboTipoMaterial.ValueMember = "CodTipoMaterial"
            End If
        End If

    End Sub

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub btnEditar_Click(sender As System.Object, e As System.EventArgs) Handles btnEditar.Click
        Try
            If dgMateriales.SelectedRows.Count > 0 Then
                Dim cod As New Integer
                cod = dgMateriales.SelectedRows(0).Cells(0).Value

                If frmMateriales Is Nothing Then

                    frmMateriales = New FrmMateriales
                    frmMateriales.codMaterial = cod
                    'If frmClientes.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    frmMateriales.ShowDialog()
                    CargarMateriales()
                    frmMateriales.Dispose()
                    frmMateriales = Nothing
                    'End If
                Else
                    frmMateriales.codMaterial = cod
                    frmMateriales.ShowDialog()
                End If

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub txtBuscar_Click(sender As System.Object, e As System.EventArgs) Handles txtBuscar.Click
        CargarMateriales()
    End Sub

    Private Sub txtNombre_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtMaterial.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarMateriales()

        End If
    End Sub

    Private Sub dgMateriales_DoubleClick(sender As System.Object, e As System.EventArgs) Handles dgMateriales.DoubleClick
        btnEditar_Click(sender, e)
    End Sub

    Private Sub dgMateriales_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles dgMateriales.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnEditar_Click(sender, e)
        End If

    End Sub

    Private Sub cbxTipoMaterial_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cbxTipo.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarMateriales()
        End If
    End Sub

    Private Sub cboTipoMaterial_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cboTipoMaterial.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarMateriales()
        End If
    End Sub

    Private Sub cboProveedor_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs)
        If e.KeyCode = Keys.Enter Then
            CargarMateriales()
        End If
    End Sub

    Private Sub cbxProveedor_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs)
        If e.KeyCode = Keys.Enter Then
            CargarMateriales()
        End If
    End Sub

    Private Sub btnNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevo.Click
        '    If frmciudad Is Nothing Then
        frmMateriales = New FrmMateriales()
        frmMateriales.ShowDialog()
        CargarMateriales()
        dgMateriales.Refresh()
    End Sub
End Class